<%-- 
    Document   : Admin
    Created on : Apr 8, 2011, 4:48:17 PM
    Author     : Widhaprasa Waliprana
--%>


<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="java.lang.Integer"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="database.database"%>
<%@page import="structure.Tree"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<%
            database db = new database();
            db.createConnection();

            ResultSet rs;
            int printID;
            printID = 0;
            List<Integer> categoryList = new ArrayList<Integer>();
            List<Integer> userList = new ArrayList<Integer>();
            List<Integer> topcategoryList = new ArrayList<Integer>();
            List<Integer> showcategoryList = new ArrayList<Integer>();

            if (request.getParameter("category") != null) {
                printID = Integer.parseInt(request.getParameter("category"));
            }

            db.select("SELECT * FROM categorydb ORDER BY categoryid ASC");
            rs = db.getResultSet();
            while (rs.next()) {
                categoryList.add(rs.getInt("categoryid"));
            }

            for (int i : categoryList) {
                if (db.getParentID(i) == printID) {
                    showcategoryList.add(i);
                }
                if (db.getParentID(i) == 0) {
                    topcategoryList.add(i);
                }
            }

            String category = "";
            if (printID != 0) {
                category += "<div class = 'categoryname'>" + db.getCategoryName(printID) + "</div>"
                        + "<div class = 'categoryupup'><a class = 'categorylink' href = 'Admin.jsp'>Up to Category</a></div>"
                        + "<form action='Admin.jsp' id='category" + printID + "' method='get' style='display: none;'>"
                        + "<input type='hidden' name='category' value='" + db.getParentID(printID) + "' />"
                        + "</form>";
                if (db.getParentID(printID) == 0) {
                    category += "<div class = 'categoryup'><a class = 'categorylink' href = 'javascript:void();' onclick = 'javascript: document.getElementById(\"category" + printID + "\") .submit()' >Up (Category)</a></div>";
                } else {
                    category += "<div class = 'categoryup'><a class = 'categorylink' href = 'javascript:void();' onclick = 'javascript: document.getElementById(\"category" + printID + "\") .submit()' >Up (" + db.getCategoryName(db.getParentID(printID)) + ")</a></div>";
                }

            } else {
                category += "<div class =  'categoryname' >Category</div>";
            }

            category += "<div class = 'categoryadd'><form method='post' action='../CategoryAdd' id='tambah' onsubmit='return validateCategoryName(name)'>"
                    + "<input type='hidden' name='category' value='" + printID + "' />"
                    + "<input type='text' name='name' />"
                    + "<label><input class='' value='Tambah' type='submit' /></label>"
                    + "</form></div>";

            List<Integer> changeparentID;

            String subcategory = "";
            if (!showcategoryList.isEmpty()) {
                for (int node : showcategoryList) {
                    changeparentID = new ArrayList<Integer>();
                    changeparentID.add(db.getParentID(node));
                    if (db.getParentID(node) != 0) {
                        changeparentID.add(0);
                    }
                    for (int categoryid : categoryList) {
                        if (!db.isCategoryDescendant(categoryid, node) && !changeparentID.contains(categoryid)) {
                            changeparentID.add(categoryid);
                        }
                    }
                    subcategory += "<div class = 'subcategorylink'><form action='Admin.jsp' id='category" + node + "' method='get'>"
                            + "<input type='hidden' name='category' value='" + node + "' />"
                            + "</form>"
                            + "<a class = 'categorylink' href = 'javascript:void();' onclick = 'javascript: document.getElementById(\"category" + node + "\") .submit()' >" + db.getCategoryName(node) + "</a></div>";
                    subcategory += "<br />"
                            + "<table><tbody><tr>"
                            + "<td><form method='post' action='../CategoryDelete' id='hapus' onsubmit=''>"
                            + "<input type='hidden' name='category' value='" + node + "' />"
                            + "<input class='' value='Delete'  type='submit' />"
                            + "</form></td>"
                            + "<td><form method='post' action='../CategoryEdit' id='ubah' onsubmit=''>"
                            + "<input type='hidden' name='category' value='" + node + "' />"
                            + "<label> Edit Name </label>"
                            + "<input type='text' name='categoryname' id='categoryname' value='" + db.getCategoryName(node) + "' />"
                            + "<label> Edit Code </label>"
                            + "<input type='text' name='categorycode' id='categorycode' value='" + db.getCategoryCode(node) + "' />";
                    if (printID != 0) {
                        subcategory += "<label> Edit Parent </label>"
                                + "<select name='categoryparent'>";
                        for (int parentid : changeparentID) {
                            if (parentid != 0) {
                                subcategory += "<option value=" + parentid + ">" + db.getCategoryName(parentid) + "</option>";
                            } else {
                                subcategory += "<option value=" + parentid + ">Category</option>";
                            }
                        }
                        subcategory += "</select>";
                    }
                    subcategory += "<input class='' value='Edit'  type='submit' />"
                            + "</form></td>"
                            + "</tr></tbody></table>";
                }
            } else {
                subcategory += "No Sub Category<br />";
            }
            subcategory += "<br />";

            String user = "";
            String ban = "";
            String unban = "";
            String usertype = "";
            String moderator = "";
            db.select("SELECT * FROM userdb");
            rs = db.getResultSet();
            while (rs.next()) {
                if (!rs.getString("role").equals("admin")) {
                    userList.add(rs.getInt("id"));
                }
            }

            for (int i : userList) {
                ban = "";
                unban = "";
                if (db.getStatusBanned(i) == 0) {
                    unban = "selected";
                } else {
                    ban = "selected";
                }
                usertype = "";
                moderator = "";
                if (db.getRole(i).equals("default")) {
                    usertype = "selected";
                } else if (db.getRole(i).equals("moderator")) {
                    moderator = "selected";
                }
                user += "<label>" + db.getUserName(i) + "</label>";
                user += "<form method='post' action='../EditUser' onsubmit='return validateRoomCount(roomcount)'>"
                        + "<input type='hidden' name='id' value='" + i + "' />"
                        + "<input type='hidden' name='category' value='" + printID + "' />"
                        + "<label> Status </label>"
                        + "<select name='status'>"
                        + "<option value='1' " + ban + " >Ban</option>"
                        + "<option value='0' " + unban + " >Unban</option>"
                        + "</select><br/>"
                        + "<label> Room Count </label>"
                        + "<input type='text' name='roomcount' id='roomcount' value='" + db.getRoomCount(i) + "' />"
                        + "<label> Role </label>"
                        + "<select name='role'>"
                        + "<option value='moderator' " + moderator + " >Moderator</option>"
                        + "<option value='user' " + usertype + " >User</option>"
                        + "</select>"
                        + "<select class ='moderatorcategory' id ='moderatorcategory" + i + "' name='categoryid'>";
                for (int j : topcategoryList) {
                    user += "<option value='" + j + "'>" + db.getCategoryName(j) + "</option>";
                }
                user += "</select>";
                if (db.getCategoryModerator(i) != 0) {
                    int catid = db.getCategoryModerator(i);
                    user += "<label> Kategori : " + db.getCategoryName(catid) + " </label>";
                }
                user += "<input class='' value='Edit' type='submit'/>"
                        + "</form>";
            }
            db.closeConnection();
%>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript" src="../script/admin.js"></script>
        <link type="text/css" rel="stylesheet" href="../style/default.css"/>
        <link type="text/css" rel="stylesheet" href="../style/admin.css"/>
        <link type="image/x-icon" rel="shortcut icon" href="../imgsrc/wedchaticon.png"/>
        <title>Admin</title>
    </head>
    <body>
        <div class="bodytopadmin">
            <br/>
            <br/>
            <div class="textlinks">
                <a class="textlink" id="loginuser"><%= session.getAttribute("loginuser")%></a>
                <a class="textlink">|</a>
                <a class="textlink" href="Logout">Logout</a>
            </div>
            <div class="labeladmin">wedChat!</div>
            <img alt="" class="wedimg" src="../imgsrc/wedminiicon.png"/>
        </div>
        <div class ="bodymidadmin">
            <div class="categorylist">
                <h2>Category List</h2>
                <div>
                    <%= category%>
                </div>
                <div>
                    <%= subcategory%>
                </div>
            </div>
            <div class="userlist">
                <h2>User List</h2>
                <%= user%>
            </div>
        </div>
        <div class="bodybottomadmin">
            <br/>
            <br/>
            <br/>
            <%@include file="../included/footer.jspf" %>
        </div>
    </body>
</html>
